package com.kaisa.activity.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.kaisa.activity.R;
import com.kaisa.activity.adapter.AllGoAdapter;
import com.kaisa.activity.bean.AllGoBean;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/12/17.
 */
public class AllGoActivity extends AppCompatActivity{
    public static final String PATH_ALLGO = "http://123.125.232.32/index.php?m=App&a=lists&";
    private String sign;
    private String proTypeId;
    private int pn = 0;


    @ViewInject(R.id.lv_allgo)
    private PullToRefreshListView allgoListView;
    @ViewInject(R.id.iv_allgo_back)
    private ImageView back;

    private List<AllGoBean.DataEntity> list = new ArrayList<AllGoBean.DataEntity>();

    private AllGoAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_allgo);
        x.view().inject(this);
        Intent intent = getIntent();
        sign = intent.getStringExtra("sign");
        proTypeId = intent.getStringExtra("proTypeId");
        initView();
        initData();
        addListener();
    }

    private void initView() {
        adapter = new AllGoAdapter(list,this);
        allgoListView.setAdapter(adapter);
    }

    private void initData() {
        x.http().get(getParams(), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                AllGoBean bean = JSONObject.parseObject(s,AllGoBean.class);
                list.addAll(bean.getData());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable throwable, boolean b) {

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {
                allgoListView.onRefreshComplete();
            }
        });
    }

    private void addListener() {
        //滑到低端的监听
        allgoListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
                initData();
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        allgoListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
               /* Intent intent = new Intent(AllGoActivity.this,ProductDetailsActivity.class);
                intent.putExtra("productId",list.get(i).getProduct_id());
                startActivity(intent);*/
            }
        });
    }


    public RequestParams getParams() {
        pn++;


        RequestParams params = new RequestParams(PATH_ALLGO);

        params.addBodyParameter("sign",sign);
        params.addBodyParameter("proTypeId",proTypeId);
        params.addBodyParameter("order","1");
        params.addBodyParameter("page",pn+"");
//        params.addBodyParameter("m","App");
//        params.addBodyParameter("a","lists");
        return params;
    }
}
